<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of FileDaoImpl
 *
 * @author krisada.thiangtham
 */
require '../com.swiss.mooh.dao/FileDao.php';
require '../com.swiss.mooh.utils/ArrayList.php';

class FileDaoImpl implements FileDao {

    public function createNewFileContent($fileID, $fileName, $filePath, $fileDescID, $fileCreateDate) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        if (is_null($fileDescID) || empty($fileDescID) || $fileDescID == "") {
            $stmt = $mysqli->prepare("INSERT INTO SWISS_FILE_CONTENT (FILE_ID,FILE_NAME,FILE_PATH,CREATED_DATE_TIME) VALUES (?,?,?,?);");
            $stmt->bind_param('ssss', $fileID, $fileName, $filePath, $fileCreateDate);
        } else {
            $stmt = $mysqli->prepare("INSERT INTO SWISS_FILE_CONTENT (FILE_ID,FILE_NAME,FILE_PATH,DESC_ID,CREATED_DATE_TIME) VALUES (?,?,?,?,?);");
            $stmt->bind_param('sssss', $fileID, $fileName, $filePath, $fileDescID, $fileCreateDate);
        }
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function deleteFileContentByID($fileID) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("DELETE FROM SWISS_FILE_CONTENT WHERE FILE_NAME = ?");
        $stmt->bind_param('s', $fileID);
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function getAllFileContentByDescID($descID) {
        require '../com.swiss.mooh.model/File.php';
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        if (is_null($descID) || empty($descID) || $descID == "") {
            $detailList = new ArrayList();
            $stmt = $mysqli->prepare("SELECT * FROM SWISS_FILE_CONTENT WHERE DESC_ID IS NULL;");
            $stmt->execute();
            $meta = $stmt->result_metadata();
            while ($field = $meta->fetch_field()) {
                $parameters[] = &$row[$field->name];
            }
            call_user_func_array(array($stmt, 'bind_result'), $parameters);
            while ($stmt->fetch()) {
                foreach ($row as $key => $val) {
                    $x[$key] = $val;
                }
                $fileVO = new File();
                $fileVO->setDescID(null);
                $fileVO->setFileCreatedDateTime($x['CREATED_DATE_TIME']);
                $fileVO->setFileID($x['FILE_ID']);
                $fileVO->setFileName($x['FILE_NAME']);
                $fileVO->setFilePath($x['FILE_PATH']);

                $detailList->Add($fileVO);
            }
            $stmt->close();
            return $detailList;
        } else {
            $detailList = new ArrayList();
            $stmt = $mysqli->prepare("SELECT * FROM SWISS_FILE_CONTENT WHERE DESC_ID LIKE ?;");
            $stmt->bind_param('s', $descID);
            $stmt->execute();
            $meta = $stmt->result_metadata();
            while ($field = $meta->fetch_field()) {
                $parameters[] = &$row[$field->name];
            }
            call_user_func_array(array($stmt, 'bind_result'), $parameters);
            while ($stmt->fetch()) {
                foreach ($row as $key => $val) {
                    $x[$key] = $val;
                }
                $fileVO = new File();
                $fileVO->setDescID($x['DESC_ID']);
                $fileVO->setFileCreatedDateTime($x['CREATED_DATE_TIME']);
                $fileVO->setFileID($x['FILE_ID']);
                $fileVO->setFileName($x['FILE_NAME']);
                $fileVO->setFilePath($x['FILE_PATH']);

                $detailList->Add($fileVO);
            }
            $stmt->close();
            return $detailList;
        }
    }

    public function getFileContentByID($fileID) {
        
    }

    public function updateFileContent($descID) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("UPDATE SWISS_FILE_CONTENT SET DESC_ID = ? WHERE DESC_ID IS NULL");
        $stmt->bind_param('s', $descID);
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function deleteAllFileTemp() {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("DELETE FROM SWISS_FILE_CONTENT WHERE DESC_ID IS NULL;");
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function getFileByDescID($descID) {
        require '../com.swiss.mooh.model/File.php';
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $detailList = new ArrayList();
        $stmt = $mysqli->prepare("SELECT * FROM SWISS_FILE_CONTENT WHERE DESC_ID LIKE ?;");
        $stmt->bind_param('s', $descID);
        $stmt->execute();
        $meta = $stmt->result_metadata();
        while ($field = $meta->fetch_field()) {
            $parameters[] = &$row[$field->name];
        }
        call_user_func_array(array($stmt, 'bind_result'), $parameters);
        while ($stmt->fetch()) {
            foreach ($row as $key => $val) {
                $x[$key] = $val;
            }
            $fileVO = new File();
            $fileVO->setDescID($x['DESC_ID']);
            $fileVO->setFileCreatedDateTime($x['CREATED_DATE_TIME']);
            $fileVO->setFileID($x['FILE_ID']);
            $fileVO->setFileName($x['FILE_NAME']);
            $fileVO->setFilePath($x['FILE_PATH']);

            $detailList->Add($fileVO);
        }
        $stmt->close();
        return $detailList;
    }

}
